export default {
    //命名空间
    namespace: 'example',

    //状态数据
    state: {
        roles:[],
        token:''
    },

    //初始化
    subscriptions: {
        setup({ dispatch, history }) {  // eslint-disable-line
            // console.log("初始化")
            dispatch({
                type:'refresh'
            })
        },
    },

    //异步
    effects: {
        *setUserInfo(action, { call, put }) {  // eslint-disable-line
            //yield关键字
            //使用call获取异步数据和进行异步处理
            //yield call(promise对象)
            //var res=yield call(query,参数)
            // var res = yield call(query)
            //使用put推给下一个reducer
            // console.log(action);
            yield put({ type: 'save' ,payload:action.payload});
        },
    },

    //同步
    //更新状态的事件
    reducers: {
        save(state, action) {
            localStorage.setItem('example', JSON.stringify({ ...state, ...action.payload }))
            return { ...state, ...action.payload };
        },
        refresh(state,action){
            const example=JSON.parse(localStorage.getItem("example"))
            return {
                ...state,
                ...example
            }
        }
    },

};

//异步操作
async function query() {
    const res=await fetch('/api/users').then(res => res.json());
    return res
}